{{!
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
}}

{{#if view.isLoaded}}

  <div id="alert-definition-details">

  <div class="alert-definition-details-header row-fluid">
    <div class="span8">
      {{! Alert Definition Name }}
      <div class="definition-name row-fluid">
        {{#if controller.editing.label.isEditing}}
          <div {{bindAttr class="controller.editing.label.isError:error :span5 :name-text-field :control-group"}}>
            {{view Em.TextField valueBinding="controller.editing.label.value" maxlength="255"}}
          </div>
          <div class="edit-buttons">
            <button {{action cancelEdit controller.editing.label target="controller"}}
                class="btn cancel-button">{{t common.cancel}}</button>
            <button {{bindAttr disabled="controller.editing.label.isError"}} {{action saveEdit controller.editing.label target="controller"}}
                class="btn btn-primary save-button">{{t common.save}}
            </button>
          </div>
        {{else}}
          <span>
            {{controller.content.label}}
          </span>
        {{/if}}
        {{#unless controller.editing.label.isEditing}}
          {{#isAuthorized "SERVICE.TOGGLE_ALERTS"}}
            <a {{action edit controller.editing.label target="controller"}} class="edit-description-button"><i
                class="icon-pencil"></i></a>
          {{/isAuthorized}}
        {{/unless}}
      </div>
      {{! Alert Definition Name end }}
      <div class="error-msg"> {{controller.errorMessage}} </div>
      <div><a href="javascript:void(null)" data-toggle="modal" {{action back}}><i
          class="icon-arrow-left"></i>&nbsp;{{t common.back}}</a>
      </div>
    </div>

    <div class="status span4">
      {{view App.AlertDefinitionSummary contentBinding="view.controller.content"}}
    </div>
  </div>

  <div class="row-fluid" style="display: flex; align-items: stretch;">

    {{! Left column }}
    {{! Alert Definition Configs }}
    <div class="box span8">
      <div class="box-header">
        <div class="pull-left">
          <h4>{{t common.configuration}}</h4>
        </div>
        <div class="pull-right span5 row-fluid" style="padding:0 10px;">
          {{#isAuthorized "SERVICE.TOGGLE_ALERTS"}}
            {{#unless App.router.mainAlertDefinitionConfigsController.canEdit}}
              <a {{action editConfigs target="App.router.mainAlertDefinitionConfigsController"}}
                  class="pull-right edit-link">
                {{t common.edit}}
              </a>
            {{/unless}}
          {{/isAuthorized}}
        </div>
      </div>
      {{view App.AlertDefinitionConfigsView contentBinding="view.controller.content" alertDefinitionTypeBinding="view.controller.content.type" canEdit=false}}
      {{#if App.router.mainAlertDefinitionConfigsController.canEdit}}
        <div class="edit-buttons">
          <button {{action cancelEditConfigs target="App.router.mainAlertDefinitionConfigsController"}}
              class="btn">{{t common.cancel}}</button>
          <button {{bindAttr disabled="App.router.mainAlertDefinitionConfigsController.hasErrors"}} {{action saveConfigs target="App.router.mainAlertDefinitionConfigsController"}}
              class="btn btn-primary">{{t common.save}}
          </button>
        </div>
      {{/if}}
    </div>
    {{! Alert Definition Configs end }}
    {{! Left column end }}

    {{! Right column }}
    <div class="span4 right-column">
      <div class="properties-list background-text">
        <div class="row-fluid">
          <div class="span6 property-name">{{t alerts.table.state}}:</div>
          <div class="span6">
            {{#if controller.content.enabled}}
              {{#isAuthorized "SERVICE.TOGGLE_ALERTS"}}
                <a href="#" {{action "toggleState" controller.content target="controller"}} {{bindAttr class="controller.content.enabled:alert-definition-enable:alert-definition-disable"}}>
                  <span
                      class="enable-disable-button" {{translateAttr data-original-title="alerts.table.state.enabled.tooltip"}}>
                    {{view.enabledDisplay}}
                  </span>
                </a>
              {{else}}
                {{view.enabledDisplay}}
              {{/isAuthorized}}
            {{else}}
              {{#isAuthorized "SERVICE.TOGGLE_ALERTS"}}
                <a href="#" {{action "toggleState" controller.content target="controller"}} {{bindAttr class="controller.content.enabled:alert-definition-enable:alert-definition-disable"}}>
                  <span
                      class="enable-disable-button" {{translateAttr data-original-title="alerts.table.state.disabled.tooltip"}}>
                    {{view.disabledDisplay}}
                  </span>
                </a>
              {{else}}
                {{view.disabledDisplay}}
              {{/isAuthorized}}
            {{/if}}
          </div>
        </div>
        {{#if controller.content.serviceDisplayName}}
          <div class="row-fluid">
            <div class="span6 property-name">{{t common.service}}:</div>
            <div class="span6"><span>{{controller.content.serviceDisplayName}}</span></div>
          </div>
        {{/if}}
        {{#if controller.content.componentNameFormatted}}
          <div class="row-fluid">
            <div class="span6 property-name">{{t common.component}}:</div>
            <div class="span6">{{controller.content.componentNameFormatted}}</div>
          </div>
        {{/if}}
        <div class="row-fluid">
          <div class="span6 property-name">{{t common.type}}:</div>
          <div class="span6">
            <span {{bindAttr class=":type-icon  controller.content.typeIconClass"}}></span> {{controller.content.type}}
          </div>
        </div>
        {{#if controller.groupsList.length}}
          <div class="row-fluid">
            <div class="span6 property-name">{{t alerts.definition.details.groups}}:</div>
            <div class="span6">
              <ul>
                {{#each groupName in controller.groupsList}}
                  <li>{{groupName}}</li>
                {{/each}}
              </ul>
            </div>
          </div>
        {{/if}}
        <div class="row-fluid">
          <div class="span6 property-name">{{t alerts.table.header.lastTrigger}}:</div>
          <div class="span6">{{controller.content.lastTriggeredFormatted}}</div>
        </div>
        {{#unless controller.content.isTypeAggregate}}
          <div class="row-fluid">
            <div class="span6 property-name">{{t alerts.table.header.repeatTolerance}}:</div>
            <div class="span6">
              {{#if controller.content.repeat_tolerance_enabled}}
                <span>
                  {{controller.content.repeat_tolerance}}
                </span>
              {{else}}
                <span>
                  {{controller.globalAlertsRepeatTolerance}}
                  {{t alerts.table.header.default}}
                </span>
              {{/if}}
              {{#isAuthorized "SERVICE.TOGGLE_ALERTS"}}
                <a {{action editRepeatTolerance target="controller"}} class="edit-description-button">
                  <i class="icon-pencil"></i>
                </a>
              {{/isAuthorized}}
            </div>
          </div>
        {{/unless}}
        {{#if controller.content.hasHelpUrl}}
          <div class="row-fluid">
            <div class="span6 property-name">{{t alerts.table.header.helpUrl}}:</div>
            <div class="span6"><label for=""><a {{bindAttr href="controller.content.helpUrl"}} target="_blank">{{t common.link}}</a></label></div>
          </div>
        {{/if}}
      </div>
    </div>
  </div>
  {{! Right column end }}

  {{! List of alerts }}
  <div class="definition-details-block">
  <span class="background-text">
    <strong>{{t alerts.definition.details.instances}}</strong>
  </span>
    <hr>
    <div>

      {{#if App.router.mainAlertInstancesController.isLoaded}}

        <table class="table advanced-header-table table-bordered table-striped alerts-table" id="alert-instances-table">
          <thead>
          {{#view view.sortView classNames="label-row" contentBinding="view.filteredContent"}}
            {{view view.parentView.serviceSort class="first service-sorting"}}
            {{view view.parentView.hostNameSort class="host-sorting"}}
            {{view view.parentView.stateSort class="state-sorting"}}
            <th>{{t alerts.definition.details.24-hour}}</th>
            <th>{{t alerts.table.header.check.response}}</th>
          {{/view}}
          <tr class="filter-row">
            <th class="first">{{view view.serviceFilterView}}</th>
            <th>{{view view.hostNameFilterView}}</th>
            <th>{{view view.stateFilterView }}</th>
            <th>{{!}}</th>
            <th>{{!}}</th>
          </tr>
          </thead>
          <tbody>
          {{#if view.pageContent}}
            {{#each instance in view.pageContent}}
              {{#view view.instanceTableRow}}
                <td class="first">
                  {{#if instance.serviceDisplayName}}
                    {{#if instance.service.isLoaded}}
                      <a {{action goToService instance.service target="view"}}
                        href="#">{{instance.serviceDisplayName}}</a>
                    {{else}}
                      {{instance.serviceDisplayName}}
                    {{/if}}
                  {{/if}}
                </td>
                <td>
                  {{#if instance.hostName}}
                    <a {{action goToHostAlerts instance.host target="view"}} href="#">
                      {{instance.hostName}}
                    </a>
                  {{/if}}
                </td>
                <td>
                  {{view App.AlertInstanceStateView instanceBinding="instance"}}
                </td>
                <td>{{view view.parentView.lastDayCount hostNameBinding="instance.hostName"}}</td>
                <td>
                  <a href="#" class="alert-text" {{action openFullResponse instance target="view"}} {{bindAttr data-original-title="instance.text"}}>
                    {{instance.text}}
                  </a>
                </td>
              {{/view}}
            {{/each}}
          {{else}}
            <tr>
              <td class="first" colspan="5">
                {{t alerts.definition.details.noAlerts}}
              </td>
            </tr>
          {{/if}}
          </tbody>
        </table>

        <div class="page-bar">
          <div class="filtered-info span4">
            {{#if view.showFilteredContent}}
              <label>{{view.filteredContentInfo}} - <a {{action clearFilters target="view"}}
                href="#">{{t tableView.filters.clearAllFilters}}</a></label>
            {{/if}}
          </div>
          <div class="items-on-page">
            <label>{{t common.show}}: {{view view.rowsPerPageSelectView selectionBinding="view.displayLength"}}</label>
          </div>
          <div class="info">{{view.paginationInfo}}</div>
          <div class="paging_two_button">
            <a {{bindAttr class="view.paginationLeftClass"}}{{action previousPage target="view"}}><i
                class="icon-arrow-left"></i></a>
            <a {{bindAttr class="view.paginationRightClass"}}{{action nextPage target="view"}}><i
                class="icon-arrow-right"></i></a>
          </div>
        </div>

      {{else}}
        {{view App.SpinnerView}}
      {{/if}}

    </div>
  </div>
  {{! List of alerts end }}

  </div>
{{else}}
  {{view App.SpinnerView}}
{{/if}}
